package com.zygk.xmpp.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.zygk.library.adapter.BaseListAdapter;
import com.zygk.library.util.JsonUtil;
import com.zygk.library.util.StringUtils;
import com.zygk.xmpp.R;
import com.zygk.xmpp.model.M_ChatItem;
import com.zygk.xmpp.view.expression.ExpressionUtil;
import java.util.List;

/**
 * Created by Administrator on 2018/7/21 0021.
 */

public class ChatAdapter extends BaseListAdapter<M_ChatItem> {

  public ChatAdapter(Context context, List<M_ChatItem> list) {
    super(context, list);
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflater.inflate(R.layout.xmpp_item_chat_list, null);
      holder.llLeft = (RelativeLayout) convertView.findViewById(R.id.ll_chat_left);
      holder.llRight = (LinearLayout) convertView.findViewById(R.id.ll_chat_right);
      holder.tvDate = (TextView) convertView.findViewById(R.id.tv_chat_date);
      holder.tvName = (TextView) convertView.findViewById(R.id.tv_chat_name);
      holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_chat_title);
      holder.tvName2 = (TextView) convertView.findViewById(R.id.tv_chat_name2);
      holder.tvTitle2 = (TextView) convertView.findViewById(R.id.tv_chat_title2);
      holder.iv_left = (ImageView) convertView.findViewById(R.id.iv_left);
      holder.iv_right = (ImageView) convertView.findViewById(R.id.iv_right);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    M_ChatItem chatItem = getData().get(position);
    holder.tvDate.setText(chatItem.getSendDate());
    M_ChatItem msg = JsonUtil.jsonToObject(chatItem.getMsg(), M_ChatItem.class);
    if (chatItem.getInOrOut() == 0) {
      holder.llLeft.setVisibility(View.VISIBLE);
      holder.llRight.setVisibility(View.GONE);
      holder.tvName.setText(chatItem.getUsername());
      if ("0".equals(msg.getBodyType())) {
        holder.tvTitle.setText(
            ExpressionUtil.getText(mContext, StringUtils.Unicode2GBK(msg.getMsg())));
      }
      //if (chatItem.getTitle() != null && !chatItem.getTitle().isEmpty()) {
      //  holder.tvTitle.setText(chatItem.getTitle());
      //  holder.tvTitle.setVisibility(View.VISIBLE);
      //  holder.iv_left.setVisibility(View.GONE);
      //} else {
      //  holder.tvTitle.setVisibility(View.GONE);
      //  holder.iv_left.setVisibility(View.VISIBLE);
      //  Glide.with(mContext).load(ALBUM_PATH + chatItem.getImg_path()).into(holder.iv_left);
      //}
    } else {
      holder.llLeft.setVisibility(View.GONE);
      holder.llRight.setVisibility(View.VISIBLE);
      holder.tvName2.setText(chatItem.getUsername());
      if ("0".equals(msg.getBodyType())) {
        holder.tvTitle2.setText(
            ExpressionUtil.getText(mContext, StringUtils.Unicode2GBK(msg.getMsg())));
      }
      //if (chatItem.getTitle() != null && !chatItem.getTitle().isEmpty()) {
      //  holder.tvTitle2.setText(chatItem.getTitle());
      //  holder.tvTitle2.setVisibility(View.VISIBLE);
      //  holder.iv_right.setVisibility(View.GONE);
      //} else {
      //  holder.tvTitle2.setVisibility(View.GONE);
      //  holder.iv_right.setVisibility(View.VISIBLE);
      //  Glide.with(mContext).load(chatItem.getImg_path()).into(holder.iv_right);
      //}
    }
    return convertView;
  }

  class ViewHolder {
    RelativeLayout llLeft;
    LinearLayout llRight;
    TextView tvDate;
    TextView tvName;
    TextView tvTitle;
    TextView tvName2;
    TextView tvTitle2;
    ImageView iv_left;
    ImageView iv_right;
  }
}
